package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGGrubReportFinal;
import com.baosight.sczx.zm.cg.domain.ZMCGTSPGDispatchFinal;

/**
 * 竖井提升和排矸情况(调度日报用)
 * <p>Title: ServiceZMCG73.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-27 上午08:01:23  严洪浩  Create
 *</pre>
 */
public class ServiceZMCG86 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG73</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG73</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		StaticTools.setQueryParameters(inInfo, "fdVirtualDate", inInfo.getString("querydate"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCGTSPGDispatchFinal.query", new ZMCGTSPGDispatchFinal());
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cg.status", new String[]{"草拟", "上报"}, new String[]{"草拟", "上报"}, true));//状态
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cg.workNum", fdMiningId, true));//班次
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cg.department", "orgCode", "orgName", true, UserSession.getInSessionProperty("miningCode").toString()));
		
		return outInfo;
	}

	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCGTSPGDispatchFinal _dom = new ZMCGTSPGDispatchFinal();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_TSPGDISPATCH_FINAL"));
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			
			//校验是否重复
			Map<String, String> params = new HashMap<String, String>();
	        boolean validate = validate(inInfo, _dom, "ZMCGTSPGDispatchFinal.validate", params);
	        if (validate) {
				out = this.query(inInfo);
				return out;
			}
	        this.dao.insert("ZMCGTSPGDispatchFinal.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCGTSPGDispatchFinal _dom = new ZMCGTSPGDispatchFinal();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));

			//校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
	        boolean validate = validate(inInfo, _dom, "ZMCGTSPGDispatchFinal.validate", params);
	        if (validate) {
				out = this.query(inInfo);
				return out;
			}
		    this.dao.update("ZMCGTSPGDispatchFinal.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo _temp = super.delete(inInfo, "ZMCGTSPGDispatchFinal.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());		
		return outInfo;
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCGTSPGDispatchFinal _dom = new ZMCGTSPGDispatchFinal();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdStatus("上报");

			this.dao.update("ZMCGTSPGDispatchFinal.report",_dom);

		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	
	/**
	 * 撤回
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:24 张志芳
	 * </p>
	 *
	 */
	public EiInfo goback(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCGGrubReportFinal _dom = new ZMCGGrubReportFinal();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			dao.delete("ZMCGTSPGDispatchFinal.delete",_dom);
			dao.update("ZMCHTSPGDispatch.goback", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "条记录成功");
		return out;	
			
	}
	
	
	/**
	 * 验证上报是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:04:59 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCGTSPGDispatchFinal _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}

		params.put("fdWorknumId", _dom.getFdWorknumId());
		params.put("fdCurrentDate", _dom.getFdCurrentDate());
		params.put("fdMiningName", _dom.getFdMiningName());
	
		List result = dao.query("ZMCGTSPGDispatchFinal.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "每矿每天每班次只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}
